package com.mh.study.dubbo;

import com.mh.study.dubbo.component.HelloComponent;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author MH
 * @since 2022-01-11
 */
public class ServiceConsumerMain {

    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        System.out.println("-------------");
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        // 获取消费者组件
        HelloComponent service = context.getBean(HelloComponent.class);
        while (true) {
            System.out.println("请输入回车");
            System.in.read();
            String hello = service.sayHello("world", 200);
            System.out.println("result :" + hello);
            // 利用Future 模式来获取
            Future<Object> future  = RpcContext.getContext().getFuture();
            System.out.println("future result:"+future.get());
        }
    }

    @Configuration
    @ComponentScan("com.mh.study.dubbo.component")
    @EnableDubbo(scanBasePackages = "com.mh.study.dubbo.service")
    @PropertySource("classpath:/dubbo-consumer.properties")
    static class ConsumerConfiguration {
    }
}
